package com.bestcem.xm.deliver.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.bp.xm.user.grpc.v1.services.Group;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.mybatis.base.ResultPage;
import com.bestcem.xm.component.mybatis.service.IXmService;
import com.bestcem.xm.deliver.controller.param.SurveyDetailParam;
import com.bestcem.xm.deliver.controller.v2.param.*;
import com.bestcem.xm.deliver.controller.v2.vo.DedicatedDeliverVo;
import com.bestcem.xm.deliver.controller.v2.vo.SurveyDeliverPageVo;
import com.bestcem.xm.deliver.entity.DeliverDedicatedDeliver;
import com.bestcem.xm.deliver.entity.DeliverDeliver;
import com.bestcem.xm.deliver.entity.DeliverDisturbRule;
import com.bestcem.xm.deliver.enums.DeliverTtypeEnum;
import com.bestcem.xm.deliver.grpc.client.dto.OrgInfoDto;
import com.bestcem.xm.qdes.grpc.v1.services.Project;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * @author been
 * @description 针对表【ds_deliver_deliver】的数据库操作Service
 * @createDate 2022-11-07 13:49:46
 */
public interface DeliverDeliverService extends IXmService<DeliverDeliver> {

    /**
     * [
     * "00:00-08:00",
     * "12:00-13:30"
     * ]
     *
     * @return boolean
     * @author chenglong.yue <chenglong.yue@idiaoyan.com>
     * @date 2022/12/27 16:56
     */
    default boolean inCheckDelay(JSONArray arry) {
        if (ObjectUtils.isEmpty(arry) || arry.isEmpty()) {
            return false;
        }
        for (int i = 0; i < arry.size(); i++) {
            String delay_time = arry.getString(i);
            String start = delay_time.split("-")[0];
            String end = delay_time.split("-")[1];

            LocalTime stime = LocalTime.of(Integer.parseInt(start.split(":")[0]), Integer.parseInt(start.split(":")[1]));
            LocalTime etime = LocalTime.of(Integer.parseInt(end.split(":")[0]), Integer.parseInt(end.split(":")[1]));
            LocalTime now = LocalDateTime.now().toLocalTime();

            if (now.isBefore(etime) && now.isAfter(stime)) {
                return true;
            }
        }
        return false;
    }

    void doDefaultDeliverNameSeq(String deliverName, String projectId, int ttype);

    List<String> getDeliverMemberIdList(String deliver_id, DeliverTtypeEnum deliveEnum);

    Object getBaseAndMemberIdListByDeliverId(String deliver_id, DeliverTtypeEnum deliveEnum);

    /**
     * 获取单个连接
     *
     * @param id    id
     * @param param param
     * @return 数据
     */
    JSONObject getDeliverById(String id, GetOneDeliverParam param);

    /**
     * 创建连接
     *
     * @param param param
     * @return 数据
     */
    String createDeliver(CreateDeliverParam param);

    /**
     * 获取连接
     *
     * @param page  page
     * @param param param
     * @return 数据
     */
    ResultPage<DedicatedDeliverVo> getDeliver(Page<DeliverDedicatedDeliver> page, GetDeliverParam param);

    /**
     * 专用链接投放 追加 投放
     *
     * @param param param
     * @return 数据
     */
    Object appendDeliver(AppendDeliverParam param);

    /**
     * 专用链接投放 追加 投放
     *
     * @param surveyId  surveyId
     * @param deliverId deliverId
     * @param param     param
     * @return 数据
     */
    String getDeliverDownloadPath(String surveyId, String deliverId, DeliverDownloadParam param);

    Integer deliverDelete(String surveyId, String deliverId, DeliverDownloadParam param);

    XmResultJson<?> singleSurveyNoDisturb(JSONObject param);

    XmResultJson<Map<String, List<Object>>> getSingleSurveyNoDisturb(String surveyId);

    String deliverLinkDownload(GetDownloadLinkParam param);

    String getDeliverTarPath(String surveyId, String deliverId, DeliverDefaultDownloadParam param, String rootGroupId,
                             String search);

    void setDeliverRespondentEnableRedis(String deliverId, boolean enableRespondent);

    String getSurveyDedicatedDwzUrl(String orgId, String surveyId, String deliverId, String dedCode,
                                    String checkCode, OrgInfoDto org, Map<String, String> kwargs);

    String getSurveyDwzLink(String link, String customDomainUrl);

    String aioConvertShortUrl(String url, String customDomainUrl);

    JSONObject getSurveyLink(String orgId, String surveyId, String deliverId, String orgCode, Map<String, String> kwargs);

    String saveMatrixImgByParam(String content, String tmpPath, String ossPath, String name, boolean keepFile);

    void itemTofront(SurveyDeliverPageVo deliverVo, Project project);

    Integer getDeliverRspdAmount(String deliverId, String groupId, List<String> groupIdList, String memberId,
                                 Integer status, List<String> dedCodeList, String projectId);

    long getDeliverPlanTotalCount(String deliverId, int ttype);

    String getQrcodePath(String orgId, Project survey, String deliverId, Group group);

    //get_no_disturb_member_id_by_deliver
    List<String> getNodisturbMemberIdByDeliver(String deliverId);

    //get_all_enable_disturb_rule
    List<DeliverDisturbRule> getAllEnableDisturbRule(String orgId, String surveyId);

    void updateDeliver(String deliverId);

    //get_no_disturb_pro
    List<Object> getNoDisturbProjects(List<DeliverDisturbRule> enable_disturb_list);

    ServiceResult<?> memberDeliverImport(MultipartFile files, String deliverId, boolean isMobile);

    JSONObject getGlobalSurveyNoDisturb(String surveyId, Boolean enable);

    void globalSurveyNoDisturb(JSONObject param);

    XmResultJson<?> putDeliversAward(String id, JSONObject param);

    void getMemberRespMaps(String project_id, String survey_resp_status_arg, String deliver_id, Integer check_submit, Map<String, Integer> member_resp_maps, List<String> member_ids);

    void fillNoStart(Map<String, Integer> member_resp_maps, List<String> mem_ids, List<String> resp_member_id_list);

    List<?> getTimeRangeSurveyIdList(Integer delayTime, String filterType);

    Object surveyDetail(DeliverDeliver deliver, SurveyDetailParam param);

    String getSurveyGroupDwzUrl(String orgId, String surveyId, String deliverId, Map<String, String> kwargs);

    String genSurveyLink(Map<String, String> tempMap, String deliverId, Map<String, String> kwargs);

    String createSurveyGroupQrcodes(String orgId, String projectId, String deliverId, String content, String fileName,
                                    boolean keepFile);

    List<JSONObject> getGroupLevel(String rootGroupId);

    String getDeliverSecret();

    <T> String exportWithDynamicData(String fileName, List<List<List<String>>> head, List<List<List<T>>> exportData, List<String> sheetNames, HttpServletResponse response, String ossPath);
}
